package com.monitor.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.monitor.dao.ContactDao;
import com.monitor.dao.RemoteSmsDao;
import com.monitor.dao.StatisticsDao;
import com.monitor.dao.UserDao;
import com.monitor.po.Contact;
import com.monitor.po.RemoteSms;
import com.monitor.po.Statistics;
import com.monitor.po.User;
import com.monitor.service.RemoteSmsService;
import com.monitor.util.CommenUtil;
import com.monitor.util.Pager;

public class RemoteSmsServiceImpl implements RemoteSmsService {
	private RemoteSmsDao remoteSmsDao;
	private ContactDao contactDao;
	private UserDao userDao;
	private StatisticsDao statisticsDao;

	@Override
	public Pager<RemoteSms> getRemoteSmsByUid(long uid, int currentPage, int pageSize) throws Exception {
		return remoteSmsDao.getRemoteSmsByUid(uid, currentPage, pageSize);
	}

	@Override
	public boolean deleteRemoteSmsById(long id) throws Exception {
		return remoteSmsDao.deleteRemoteSmsById(id) > 0;
	}

	@Override
	public boolean deleteRemoteSms(String idStr) throws Exception {
		if (CommenUtil.isEmpty(idStr)) {
			return false;
		}
		String[] arr = idStr.split(",");
		long[] ids = new long[arr.length];
		for (int i = 0; i < arr.length; i++) {
			String sid = arr[i];
			long id = Long.parseLong(sid);
			ids[i] = id;
		}
		return remoteSmsDao.deleteRemoteSms(ids) > 0;
	}

	@Override
	public boolean addRemoteSms(String phoneStr, String content, long uid) throws Exception {
		if (CommenUtil.isEmpty(phoneStr) || CommenUtil.isEmpty(content)) {
			return false;
		}
		String[] phones = phoneStr.split(",");
		List<Contact> contacts = new ArrayList<Contact>();
		for (String phone : phones) {
			if (CommenUtil.isEmpty(phone))
				continue;
			Contact contact = new Contact();
			contact.setPhone(phone);
			contact.setName("");
			contacts.add(contact);
			RemoteSms sms = new RemoteSms();
			sms.setContent(content);
			sms.setOwnerUserId(uid);
			sms.setTargetPhone(phone);
			remoteSmsDao.addRemoteSms(sms);
		}
		//contactDao.addContact(uid, contacts);
		// 更新用户今日发送短信条数
		User user = userDao.getUserById(uid);
		if (user != null) {
			user.setSendSmsCount(user.getSendSmsCount() + contacts.size());
			userDao.updateUser(user);
		}
		return true;
	}

	@Override
	public int getSentRemoteSmsCount(long uid) throws Exception {
		Statistics statistics = statisticsDao.getStatisticsByUid(uid);
		return statistics == null ? 0 : statistics.getSentSmsCount();
	}

	@Override
	public int getUnSentRemoteSmsCount(long uid) throws Exception {
		return remoteSmsDao.getUnSentRemoteSmsCount(uid);
	}

	public void setRemoteSmsDao(RemoteSmsDao remoteSmsDao) {
		this.remoteSmsDao = remoteSmsDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setStatisticsDao(StatisticsDao statisticsDao) {
		this.statisticsDao = statisticsDao;
	}

}
